package com.tust.app.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tust.app.consts.Constants;
import com.tust.app.domain.Answer;
import com.tust.app.domain.Topic;
import com.tust.app.service.AnswerService;
import com.tust.app.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class BBSController {

    @Autowired
    private AnswerService answerService;

    @Autowired
    private TopicService topicService;

    /*
    * 前台留言板
    * */

    @RequestMapping("/editTopic")
    public String editTopic(Integer id,String title,String detail) {
        try {
            Topic topic = new Topic();
            topic.setId(id);
            topic.setTitle(title);
            topic.setDetail(detail);
            topicService.updateByPrimaryKeySelective(topic);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("编辑帖子出错！");
        }
        return "forward:/selectAllTopic";
    }

    @RequestMapping("/selectTopicById")
    public String selectTopicById(Integer id ,HttpServletRequest request) {
        try {
            Topic topic = topicService.selectByPrimaryKey(id);
            request.setAttribute("topic", topic);
        }catch (Exception e) {
            e.printStackTrace();
            System.out.println("获取对应id帖子出错！");
        }
        return "frontPage/edittopic";
    }

    @RequestMapping("/deleteTopic2")
    public String deleteTopic2(Integer id) {
        try {
            topicService.deleteByPrimaryKey(id);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("删除个人帖子出错！");
        }
        return "forward:/selectAllTopic";
    }

    @RequestMapping("/addTopic")
    public String addTopic(String title,String detail,HttpServletRequest request) {
        try {
            String username = (String) request.getSession().getAttribute("username");
            Topic topic = new Topic();
            topic.setTitle(title);
            topic.setDetail(detail);
            topic.setEdituser(username);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String data = sdf.format(new Date());
            topic.setPubtime(data);
            topic.setState(1);
            topicService.insert(topic);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("发布帖子出错！");
        }

        return "forward:/selectAllTopic";
    }

    @RequestMapping("/selectAllTopic")
    public String selectAllTopic(HttpServletRequest request) {
        try{
            String username = (String) request.getSession().getAttribute("username");
            List<Topic> topics = topicService.selectByUserName(username);
            PageInfo page = new PageInfo(topics);
            request.setAttribute("topicList", page);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("获取个人帖子出错！");
        }

        return "frontPage/topicList";
    }

    @RequestMapping("/addAnswer")
    public String addAnswer(Integer topicId,String content,HttpServletRequest request) {

        try {
            String username = (String) request.getSession().getAttribute("username");
            Answer answer = new Answer();
            answer.setAnsweruser(username);
            answer.setTopicid(topicId);
            answer.setContent(content);
            answer.setState2(1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String data = sdf.format(new Date());
            answer.setPubtime2(data);
            answerService.insert(answer);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("添加回复出错！");
        }
        return "forward:/selectAllTopic2";
    }

    @RequestMapping("/selectTopicAnswerById")
    public String selectTopicAnswerById(Integer id, HttpServletRequest request) {
        try {
            Topic topic = topicService.selectByPrimaryKey(id);
            List<Answer> answers = answerService.selectByTopicId(id);
            PageInfo page = new PageInfo(answers);
            request.setAttribute("list", topic);
            request.setAttribute("lists", page);
            request.setAttribute("replyNum", answers.size());
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("获取帖子详情出错！");
        }
        return "frontPage/detail";
    }

    @RequestMapping("/selectAllTopic2")
    public String selectAllTopic2(Integer pn, HttpServletRequest request) {
        try {
            String username = (String) request.getSession().getAttribute("username");
            if (username == null){
                return "forward:/checkLogin";
            }
            if (pn == null){
                pn = 1;
            }
            PageHelper.startPage(pn, Constants.topicPageSize);
            List<Topic> topics = topicService.topicsList();
            PageInfo page = new PageInfo(topics);
            request.setAttribute("topicLists", page);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("展示帖子列表出错！");
        }
        return "frontPage/messageList";
    }

    /*
    * 后台互动管理
    * */

    @RequestMapping("/selectAllTopic3")
    public String selectAllTopic(Integer pn, HttpServletRequest request){
        try {
            if (pn == null){
                pn = 1;
            }
            PageHelper.startPage(pn, Constants.topicPageSize);
            List<Topic> topics = topicService.topicsList();
            PageInfo page = new PageInfo(topics);
            request.setAttribute("topicLists", page);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("展示帖子列表出错！");
        }
        return "admin/bbs/allTopic";
    }

    @RequestMapping("/selectByNamet")
    public String selectByNamet(String title, HttpServletRequest request){
        try {
            PageHelper.startPage(Constants.topicPageNum, Constants.topicPageSize);
            List<Topic> topics = topicService.selectByTitle(title);
            System.out.println(topics);
            PageInfo page = new PageInfo(topics);
            request.setAttribute("topicLists", page);

           return "admin/bbs/allTopic";

        }catch (Exception e){
            e.printStackTrace();
            System.out.println("查询帖子异常！");
        }
        return "error";
    }


    @RequestMapping("/updateState")
    public String updateState(Integer id){
        try{
            Topic topic = topicService.selectByPrimaryKey(id);
            if (topic.getState() == 0){
                topic.setState(1);
            }
            else if (topic.getState() == 1){
                topic.setState(0);
            }
            topicService.updateByPrimaryKeySelective(topic);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("修改帖子状态异常！");
        }
        return "forward:/selectAllTopic3";
    }

    @RequestMapping("/deleteTopic")
    public String deleteTopic(Integer id){
        try {
            topicService.deleteByPrimaryKey(id);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("根据id删除帖子异常！");
        }
        return "forward:/selectAllTopic3";
    }

    @RequestMapping("/selectAllAnswer")
    public String selectAllAnswer(Integer pn, HttpServletRequest request){
        try {
            if (pn == null){
                pn = 1;
            }
            PageHelper.startPage(pn, Constants.answerPageSize);
            List<Answer> answers = answerService.answersList();
            PageInfo page = new PageInfo(answers);
            request.setAttribute("answerLists", page);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("展示留言列表出错！");
        }
        return "admin/bbs/allAnswers";
    }

    @RequestMapping("/updateAnswerState")
    public String updateAnswerState(Integer id){
        try{
            Answer answer = answerService.selectByPrimaryKey(id);
            if (answer.getState2() == 0){
                answer.setState2(1);
            }
            else if (answer.getState2() == 1){
                answer.setState2(0);
            }
            answerService.updateByPrimaryKeySelective(answer);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("修改留言状态异常！");
        }
        return "forward:/selectAllAnswer";
    }

    @RequestMapping("/deleteAnswer")
    public String deleteAnswer(Integer id){
        try {
            answerService.deleteByPrimaryKey(id);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("根据id删除留言异常！");
        }
        return "forward:/selectAllAnswer";
    }

    @RequestMapping("/selectByNamea")
    public String selectByNamea(String username, HttpServletRequest request){
        try {
            PageHelper.startPage(Constants.answerPageNum, Constants.answerPageSize);
            List<Answer> answers = answerService.serchByUserName(username);
            PageInfo page = new PageInfo(answers);
            request.setAttribute("answerLists", page);
            if (answers != null){
                return "admin/bbs/allAnswers";
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("查询留言异常！");
        }
        return "error";
    }
}
